<template>
  <div class="page-content">
    <s-table
      :dataList="dataList"
      v-bind="contentTableConfig"
      :dataTotal="dataCount"
      v-model:page="pageInfo"
    >
      <!-- 头部的插槽 -->
      <template #headerHandler>
        <el-button
          icon="el-icon-refresh"
          circle
          @click="freshClick"
        ></el-button>
        <el-button
          v-if="isCreate"
          type="primary"
          size="medium"
          @click="handleCreateBtn"
          >{{ contentTableConfig.btnName ?? '' }}</el-button
        >
      </template>

      <!-- 列的插槽 -->
      <!-- 固定的共有的动态插槽 -->
      <template #status="scope">
        <el-button
          :type="scope.row.enable || scope.row.status ? 'success' : 'danger'"
          plain
          size="mini"
          >{{
            scope.row.enable
              ? '启用'
              : '禁用' || scope.row.status
              ? '有货'
              : '售罄'
          }}</el-button
        >
      </template>
      <template #createAt="scope">
        {{ $filter.formatTimeUtc(scope.row.createAt) }}
      </template>
      <template #updateAt="scope">
        {{ $filter.formatTimeUtc(scope.row.updateAt) }}
      </template>
      <template #operation="scope">
        <el-button
          v-if="isUpdate"
          icon="el-icon-edit"
          type="text"
          size="mini"
          @click="handleUpdateBtn(scope.row)"
          >编辑</el-button
        >
        <el-popconfirm
          title="确定删除吗"
          cancelButtonType="warning"
          @confirm="handleDeleteBtn(scope.row)"
        >
          <template #reference>
            <el-button
              v-if="isDelete"
              icon="el-icon-delete"
              type="text"
              size="mini"
              >删除</el-button
            >
          </template>
        </el-popconfirm>
      </template>

      <!-- 由三级组件(user、goods)动态传进来剩下的插槽 -->
      <template
        v-for="item in otherPropSlots"
        :key="item.prop"
        #[item.slotName]="scope"
      >
        <template v-if="item.slotName">
          <!-- 二级插槽 -->
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
      </template>
    </s-table>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, watch } from 'vue'

import { useStore } from '@/store'
import { usePermission } from '@/hooks/usePermission'

import STable from '@/base-ui/table'

export default defineComponent({
  components: {
    STable
  },
  props: {
    contentTableConfig: {
      type: Object,
      required: true
    },
    pageName: {
      type: String,
      required: true
    }
  },
  emit: ['createBtnClick', 'updateBtnClick'],
  setup(props, { emit }) {
    const store = useStore()
    // 0.获取角色的增删改查的权限
    const isCreate = usePermission(props.pageName, 'create')
    const isDelete = usePermission(props.pageName, 'delete')
    const isUpdate = usePermission(props.pageName, 'update')
    const isQuery = usePermission(props.pageName, 'query')

    // 1.分页器：双向绑定pageInfo
    const pageInfo = ref({ currentPage: 1, pageSize: 10 })
    watch(pageInfo, () => getPageData())

    // 2.pageContent的刷新按钮
    const freshClick = () => {
      getPageData()
    }

    // 3.发送网络请求
    const getPageData = (queryInfo: any = {}) => {
      // 先判断角色有没有获取对应页面数据的权限，有的情况下才能进行对应的网络请求
      // if (!isQuery) return
      store.dispatch('system/getPageListAction', {
        pageName: props.pageName,
        queryInfo: {
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
          size: pageInfo.value.pageSize,
          ...queryInfo
        }
      })
    }
    getPageData()

    // 4.从store里拿到数据
    const dataList = computed(() =>
      store.getters['system/pageListData'](props.pageName)
    )
    // 数据的总条数
    const dataCount = computed(() =>
      store.getters['system/pageListCount'](props.pageName)
    )

    // 5.获取排除共有的动态插槽
    const otherPropSlots = computed(() =>
      props.contentTableConfig?.propList.filter((item: any) => {
        if (item.slotName === 'status') return false
        if (item.slotName === 'createAt') return false
        if (item.slotName === 'updateAt') return false
        if (item.slotName === 'operation') return false
        return true
      })
    )

    // 6.删除按钮处理逻辑
    const handleDeleteBtn = (item: any) => {
      store.dispatch('system/deletePageDataAction', {
        pageName: props.pageName,
        id: item.id
      })
    }
    // 7.编辑按钮处理逻辑
    const handleUpdateBtn = (item: any) => {
      emit('updateBtnClick', item)
    }
    // 8.新增按钮处理逻辑
    const handleCreateBtn = () => {
      emit('createBtnClick')
    }

    // 9.select数据行选中处理
    // const handleSelectChange = (value: any) => {
    //   const idItem = value.map((item: any) => item.id)
    //   store.commit('system/saveIdList', idItem)
    // }
    // const selectDeleteClick = () => {
    //   store.dispatch('system/deletePageDataAction', {
    //     pageName: props.pageName,
    //     id: store.state.system.idList
    //   })
    // }

    return {
      getPageData,
      dataList,
      freshClick,
      dataCount,
      pageInfo,
      otherPropSlots,
      isCreate,
      isDelete,
      isUpdate,
      handleDeleteBtn,
      handleUpdateBtn,
      handleCreateBtn
      // handleSelectChange,
      // selectDeleteClick
    }
  }
})
</script>

<style scoped>
.page-content {
  border-top: 20px solid #f5f5f5;
  padding: 20px;
}
</style>
